package com.woldier.filesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.woldier.base.exception.DoopfsException;
import com.woldier.filesystem.mapper.RepositoryMapper;
import com.woldier.filesystem.model.po.File;
import com.woldier.filesystem.model.po.Repository;
import com.woldier.filesystem.service.FileService;
import com.woldier.filesystem.service.RepositoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

@Service
@RequiredArgsConstructor
public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, Repository> implements RepositoryService {

    /*Bean注入*/
    private final FileService fileService;

    @Override
    public boolean exist(String name, String name_repo) {
        if(this.list(new LambdaQueryWrapper<Repository>().eq(Repository::getUserName,name).eq(Repository::getNameRepo,name_repo)).isEmpty())
            return false;
        return true;
    }

    /**
    * @description 删除用户仓库
    * @param owner
     * @param repo
    * @return java.util.List<java.lang.String>
    * @author: woldier
    * @date: 2023/3/6 9:28
    */
    @Override
    @Transactional
    public void delete_repo(String owner, String repo) throws DoopfsException {
        String nameRepo = owner+"/"+repo;
        /*检查仓库是否存在*/
        if(!this.exist(owner, nameRepo)) DoopfsException.cast("仓库不存在");
        /*删除仓库*/
        LambdaQueryWrapper<Repository> removeLambda = new LambdaQueryWrapper<>();
        removeLambda.eq(Repository::getNameRepo,nameRepo);
        if(!this.remove(removeLambda)) DoopfsException.cast("删除仓库时出现问题");
        /*删除文件*/
        LambdaQueryWrapper<File> removeLambda2 = new LambdaQueryWrapper<>();
        removeLambda2.eq(File::getNameRepo,nameRepo);
        if(!fileService.remove(removeLambda2)) DoopfsException.cast("删除文件时出现问题");
        /*删除hadoop项目文件夹*/
        //TODO
        //restTemplate.delete(hadoopProxy+nameRepo+"?op="+ HadoopOps.DELETE+"&user.name=root&recursive=true");

    }
}
